package org.whuims.pois.report;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import org.whuims.pois.action.result.ClusterVo;
import org.whuims.pois.model.Cluster;

import java.io.IOException;
import java.util.Date;

/**
 * 需要考虑在Linux上字体 问题
 * Created by Leo on 2015/5/23 0023.
 */
public class Documents {
    public static Paragraph title() {
        Paragraph p = new Paragraph("舆情报告", font("宋体", 28, Font.BOLD));
        p.setAlignment(Element.ALIGN_CENTER);
        p.add("\n");
        p.add("\n");
        return p;
    }

    /**
     * 得到基本信息部分
     *
     * @param title
     * @param start
     * @param end
     * @return
     */
    public static Paragraph info(String title, String start, String end) {
        Paragraph p = new Paragraph();
        p.setPaddingTop(20);
        p.setIndentationLeft(0);
        p.setIndentationRight(0);
        p.setAlignment(Element.ALIGN_LEFT);
        PdfPTable pdfPTable = new PdfPTable(2);

        Phrase topicTitle = new Phrase("主题", font("宋体", 14, Font.BOLD));
        Phrase timeTitle = new Phrase("时间", font("宋体", 14, Font.BOLD));
        Phrase topic = new Phrase(title, font("宋体", 14));
        Phrase time = new Phrase(start + " 至 " + end, font("宋体", 14));
        PdfPCell topicTCell = cell_no_border(topicTitle, BaseColor.LIGHT_GRAY);
        PdfPCell topicCell = cell_no_border(topic, BaseColor.LIGHT_GRAY);
        PdfPCell timeTCell = cell_no_border(timeTitle, BaseColor.LIGHT_GRAY);
        PdfPCell timeCell = cell_no_border(time, BaseColor.LIGHT_GRAY);
        pdfPTable.addCell(topicTCell);
        pdfPTable.addCell(topicCell);
        pdfPTable.addCell(timeTCell);
        pdfPTable.addCell(timeCell);

        p.add(pdfPTable);
        return p;
    }

    /**
     * 基本统计信息
     *
     * @return
     */
    public static Paragraph basic_statistic(int total, int news, int bbs, int weibo, int weixin, int blog) {
        Paragraph p = new Paragraph();
        p.setPaddingTop(50);
        p.setIndentationLeft(30);
        p.setIndentationRight(30);

        Phrase title = phrase_title("该时段内舆情信息统计如下：");
        p.add("\n");
        p.add(title);
        PdfPTable t = new PdfPTable(2);
        t.addCell(cell(phrase_bold("类型")));
        t.addCell(cell(phrase_bold("数量")));
        t.addCell(cell(phrase("总数")));
        t.addCell(cell(phrase("" + total)));
        t.addCell(cell(phrase("新闻")));
        t.addCell(cell(phrase("" + news)));
        t.addCell(cell(phrase("论坛")));
        t.addCell(cell(phrase("" + bbs)));
        t.addCell(cell(phrase("微博")));
        t.addCell(cell(phrase("" + weibo)));
        t.addCell(cell(phrase("微信")));
        t.addCell(cell(phrase("" + weixin)));
        t.addCell(cell(phrase("博文")));
        t.addCell(cell(phrase("" + blog)));
        t.setHorizontalAlignment(Element.ALIGN_CENTER);
        p.add(t);
        return p;
    }

    /**
     * 热点话题列表
     *
     * @param list
     * @return
     */
    public static Paragraph topic(java.util.List<ClusterVo> list) {
        Paragraph p = new Paragraph();
        p.setIndentationLeft(30);
        p.setIndentationRight(30);
        p.add("\n");
        p.add(phrase_title("该时段内热点话题个数为[" + list.size() + "]" + (list.size() > 0 ? "，具体如下：" : ".")));
        PdfPTable t = new PdfPTable(1);
        for (int i = 0; i < list.size(); i++) {
            t.addCell(cell_bottom_line(topic_item(list.get(i))));

        }
        p.add(t);
        return p;
    }

    private static Phrase topic_item(ClusterVo vo) {
        Phrase item = new Phrase();
        Phrase title = new Phrase(vo.getTitle() + "   ", font("宋体", 12, Font.BOLD, 0, 0, 177, 100));
        Chunk hot = new Chunk("    " + vo.getCount() + "    ", font("宋体", 10));

        hot.setBackground(new BaseColor(177, 214, 50));
        Phrase time = phrase("   " + vo.getCrawltime() + "\n");
        Phrase abs = new Phrase(vo.getAbstext(), font("宋体", 10, Font.NORMAL));
        item.add(title);
        item.add(hot);
        item.add(time);
        item.add(abs);
        return item;
    }

    /**
     * 生成图表的Paragraph对象
     *
     * @param type 新闻、论坛等
     * @param url1 新闻走势文件地址
     * @param url2 站点分布文件地址
     * @return
     */
    public static Paragraph image(String type, String url1, String url2) {
        Paragraph p = new Paragraph();
        p.setIndentationLeft(30);
        p.setIndentationRight(30);
        p.add("\n");
        p.add(new Paragraph(phrase_title(" " + type + "  的分布图如下：\n")));
        try {
            Paragraph p1 = new Paragraph();
            Image image1 = Image.getInstance(url1);
            p1.add(image1);
            p.add(p1);
            p.add("\n");
            Paragraph p2 = new Paragraph();
            Image image2 = Image.getInstance(url2);
            p2.add(image2);
            p.add(p2);
            return p;
        } catch (BadElementException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Paragraph foot() {
        Paragraph p = new Paragraph();
        p.setAlignment(Paragraph.ALIGN_RIGHT);
        p.add("\n");
        p.add(new Chunk("报告生成时间：", font("宋体", 12, Font.NORMAL, 128, 128, 128, 100)));
        p.add(new Chunk(new Date().toLocaleString(), font("宋体", 12, Font.NORMAL, 128, 128, 128, 100)));
        p.add("\n");
        p.add(new Chunk("BY 武汉大学舆情信息系统", font("宋体", 12, Font.NORMAL, 128, 128, 128, 100)));
        return p;
    }

    private static PdfPCell cell_bottom_line(Phrase content) {
        PdfPCell cell = new PdfPCell(content);
        cell.setPadding(3);
        cell.setBorderWidth(0);
        cell.setBorderWidthBottom(1);
        cell.setPaddingBottom(5);
        cell.setColspan(3);
        return cell;
    }

    public static Phrase phrase(String text) {
        return new Phrase(text, font("宋体", 12, Font.NORMAL));
    }


    public static Phrase phrase(String text, int size, BaseColor color) {
        Font font = font("宋体", size, Font.NORMAL);
        if (color != null)
            font.setColor(color);
        return new Phrase(text, font);
    }

    public static Phrase phrase_title(String text) {
        Phrase phrase = new Phrase(text, font("宋体", 14, Font.BOLD));
        return phrase;
    }

    public static Phrase phrase_bold(String text) {
        return new Phrase(text, font("宋体", 12, Font.BOLD));
    }

    public static PdfPCell cell(Phrase content) {
        return cell(content, null, 3, 1, null, Element.ALIGN_CENTER, Element.ALIGN_CENTER);
    }

    /**
     * 无边框
     *
     * @param content
     * @param color
     * @return
     */
    public static PdfPCell cell_no_border(Phrase content, BaseColor color) {
        return cell(content, color, 3, 0, null, Element.ALIGN_CENTER, Element.ALIGN_CENTER);
    }

    public static PdfPCell cell(Phrase content, BaseColor background, int padding, int border, BaseColor border_Color, int v_align, int h_align) {
        PdfPCell cell = new PdfPCell(content);
        if (background != null) {
            cell.setBackgroundColor(background);
        }
        cell.setPadding(padding);
        cell.setBorderWidth(border);
        cell.setBorderColor(border_Color);
        cell.setVerticalAlignment(v_align);
        cell.setHorizontalAlignment(h_align);
        return cell;
    }

    /**
     * 只指定字体，其他默认
     *
     * @param bf
     * @return
     */
    public static Font font(String bf) {
        return font(bf, 16);
    }

    /**
     * 返回字体，默认样式和颜色
     *
     * @param bf
     * @param size
     * @return
     * @see #font(String, int, int)
     */
    public static Font font(String bf, int size) {
        return font(bf, size, Font.NORMAL);
    }

    /**
     * 使用默认颜色，应该是黑色
     *
     * @param bf
     * @param size
     * @param style
     * @return
     * @see #font(String, int, int, int, int, int, int)
     */
    public static Font font(String bf, int size, int style) {
        return font(bf, size, style, -1, -1, -1, -1);
    }

    /**
     * 返回字体
     *
     * @param bf    字体名称，目前只支持宋体
     * @param size  大小
     * @param style 样式：参考{@link Font},小于0则默认为0
     * @param r     颜色R 包括下面的四个值有一个小于0或者大于255则无效
     * @param g     颜色G
     * @param b     颜色B
     * @param a     透明度a
     * @return
     */
    public static Font font(String bf, int size, int style, int r, int g, int b, int a) {
        BaseFont baseFont = null;
        try {
            baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        } catch (DocumentException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        if (style <= 0)
            style = 0;  //正常样式
        if (r < 0 || g < 0 || b < 0 || a < 0 || r > 255 || g > 255 || b > 255 || a > 255) {
            return new Font(baseFont, size, style);
        }
        BaseColor bc = new BaseColor(r, g, b, a);
        Font font = new Font(baseFont, size, style, bc);
        return font;
    }
}


//字体问题
/**
 * SIMSUN.TTC:宋体和新宋体
 * SIMKAI.TTF:楷体
 * SIMHEI.TTF:黑体
 * SIMFANG.TTF:仿宋体
 */